/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_SD.h
 *
 *  Created on: 2022年7月24日
 *      Author: 64435
 */

#ifndef PERIPHERAL_LAYER_INC_LOS_SD_H_
#define PERIPHERAL_LAYER_INC_LOS_SD_H_

#include "LOS_SYS.h"

#define LOS_SPI_SD LOS_SPI_NUM3
#define CS_SD CS_1
#define CS_NULL CS_5

#define CMD0 0U
#define CMD1 1U
#define CMD5 5U
#define CMD6 6U
#define CMD8 8U
#define CMD9 9U
#define CMD10 10U
#define CMD12 12U
#define CMD13 13U
#define CMD16 16U
#define CMD17 17U
#define CMD18 18U
#define CMD24 24U
#define CMD25 25U
#define CMD27 27U
#define CMD28 28U
#define CMD29 29U
#define CMD30 30U
#define CMD32 32U
#define CMD33 33U
#define CMD38 38U
#define CMD42 42U
#define CMD55 55U
#define CMD56 56U
#define CMD58 58U
#define CMD59 59U

#define ACMD13 13U
#define ACMD18 18U
#define ACMD22 22U
#define ACMD23 23U
#define ACMD25 25U
#define ACMD26 26U
#define ACMD38 38U
#define ACMD41 41U
#define ACMD42 42U
#define ACMD51 51U

#define SD_TYPE_V2HC 1
#define SD_TYPE_V2 2

extern uint8_t SD_Type;
extern uint32_t SD_Capacity;

uint16_t LOS_SD_Init(void);
uint16_t LOS_SD_SendCMD(uint16_t cmd, uint16_t* arg, uint16_t crc);
uint16_t LOS_SD_SendCMDNoPullCS(uint16_t cmd, uint16_t* arg, uint16_t crc);
uint16_t LOS_SD_WriteReadByte(uint16_t byte);
uint16_t LOS_SD_WriteReadLastByte(uint16_t byte);
uint8_t LOS_SD_8CLK();
uint8_t LOS_SD_ReceiveData(uint8_t* data, uint32_t length, uint8_t CSPULL);
uint8_t LOS_SD_GetCID(uint8_t* cid);
uint8_t LOS_SD_GetCSD(uint8_t* csd);
uint8_t LOS_SD_ReadSingleBlock(uint32_t addr, uint8_t* buffer);
uint8_t LOS_SD_ReadMultiBlock(uint32_t addr, uint8_t* buffer, uint16_t count);
uint8_t LOS_SD_WaitReady();
uint8_t LOS_SD_WriteSingleBlock(uint32_t addr, uint8_t* data);
uint8_t LOS_SD_WriteMultiBlock(uint32_t addr, uint8_t* data, uint16_t count);
uint32_t LOS_SD_GetCapacity();

#endif /* PERIPHERAL_LAYER_INC_LOS_SD_H_ */
